package com.yoomiito.app.widget.dialog;

import android.content.Context;
import android.graphics.drawable.GradientDrawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.yoomiito.app.R;
import com.yoomiito.app.utils.DensityUtil;

public class NewVersionDialog extends BaseDialog implements View.OnClickListener {

    private TextView mVersionSizeTv;
    private TextView mVersionContent;
    private TextView mCancelTv;
    private String size;
    private String content;
    private String versionName;
    private OnItemClickListener mOnClickListener;
    private View mLineTv;
    private boolean flag = false;
    private TextView mVersionNameTv;

    public NewVersionDialog(@NonNull Context context) {
        this(context, 0);
    }

    public NewVersionDialog(@NonNull Context context, int themeResId) {
        super(context, R.style.dialog_2);
    }

    protected NewVersionDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    public View setView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_new_version, null);
        mVersionSizeTv = view.findViewById(R.id.dialog_new_version_size);
        mVersionContent = view.findViewById(R.id.dialog_new_version_content);
        mVersionNameTv = view.findViewById(R.id.dialog_new_version_name);
        GradientDrawable drawable = (GradientDrawable) mVersionNameTv.getBackground();
        drawable.setCornerRadius(DensityUtil.dp2px(15));
        mVersionSizeTv.setText(size);
        mVersionContent.setText(content);
        mVersionNameTv.setText(versionName);
        mVersionContent.setMovementMethod(ScrollingMovementMethod.getInstance());

        mCancelTv = view.findViewById(R.id.item_cancel);
        mLineTv = view.findViewById(R.id.item_line);
        mCancelTv.setOnClickListener(this);
        view.findViewById(R.id.item_sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnClickListener != null) {
                    mOnClickListener.onClick(true);
                }
            }
        });

        //是否强制更新
        setCanceledOnTouchOutside(!flag);
        if (!flag){
            view.findViewById(R.id.item_cancel).setVisibility(View.GONE);
            view.findViewById(R.id.item_line).setVisibility(View.GONE);
        }
        int v = flag?View.GONE:View.VISIBLE;
        mCancelTv.setVisibility(v);
        mLineTv.setVisibility(v);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_cancel:
                dismiss();
                break;

        }
    }

    public NewVersionDialog setSize(String size) {
        this.size = size;
        if (mVersionSizeTv != null) {
            mVersionSizeTv.setText(size);
        }
        return this;
    }

    public NewVersionDialog setContent(String content) {
        this.content = content;
        if (mVersionContent != null) {
            mVersionContent.setText(content);
        }
        return this;
    }

    public NewVersionDialog setOnClickListener(OnItemClickListener onClickListener) {
        mOnClickListener = onClickListener;
        return this;

    }

    /**
     *
     * @param flag true 强制更新 false不强制更新
     * @return
     */
    public NewVersionDialog setMustUpdate(boolean flag) {
        this.flag = flag;
        setCanceledOnTouchOutside(!flag);
        int v = flag?View.GONE:View.VISIBLE;
        if (mCancelTv !=null){
            mCancelTv.setVisibility(v);
            mLineTv.setVisibility(v);
        }
        return this;
    }

    public NewVersionDialog setVersionName(String versionName) {
        if (mVersionNameTv!=null){
            mVersionNameTv.setText(versionName);
        }
        this.versionName = versionName;
        return this;
    }

    @Override
    public void onBackPressed() {
        if (!flag){
            super.onBackPressed();
        }
    }

    public interface OnItemClickListener {
        void onClick(boolean isSure);
    }
}
